package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelRepository travelRepository;
    @Autowired
    private MongoTemplate template;

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;

    @Override
    public void save(Travel travel) {
        travel.setId(null);
        travelRepository.save(travel);
    }

    @Override
    public void update(Travel travel) {
        travelRepository.save(travel);
    }

    @Override
    public void delete(String id) {
        travelRepository.deleteById(id);
    }

    @Override
    public Travel get(String id) {
        Travel travel = travelRepository.findById(id).orElse(null);
        if (travel!=null){
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
        }

        return travel;
    }

    @Override
    public List<Travel> list() {
        return travelRepository.findAll();
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {
        //查询对象
        Query query = new Query();
        if (StringUtils.hasLength(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //出行时间
        TravelCondition day = qo.getDay();
        if (day != null) {
            query.addCriteria(Criteria.where("day").gte(day.getMin()).lte(day.getMax()));
        }
        //人均消费
        TravelCondition ex = qo.getPerExpend();
        if (ex != null) {
            query.addCriteria(Criteria.where("perExpend").gte(ex.getMin()).lte(ex.getMax()));
        }

        Pageable pageable = null;
        if (qo.getOrderType() == 1) {
            pageable = PageRequest.of(qo.getCurrentPage() - 1,
                    qo.getPageSize(), Sort.Direction.DESC, "createTime");
        } else if (qo.getOrderType() == 2) {
            pageable = PageRequest.of(qo.getCurrentPage() - 1,
                    qo.getPageSize(), Sort.Direction.DESC, "viewnum");
        } else {
            pageable = PageRequest.of(qo.getCurrentPage() - 1,
                    qo.getPageSize(), Sort.Direction.ASC, "_id");
        }


        Page<Travel> page = DBHelper.query(template, Travel.class, query, pageable);
        for (Travel travel : page) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }

        return page;
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        //目的地名称
        Destination dest = destinationService.get(travel.getDestId());
        travel.setDestName(dest.getName());
        travel.setLastUpdateTime(new Date());
        if (StringUtils.hasLength(travel.getId())) {
            //有则编辑
            this.update(travel);
        } else {
            //无则添加
            travel.setCreateTime(new Date());
            this.save(travel);
        }
    }

    @Override
    public void audit(String id, int state) {
        //获取当前游记
        Travel travel = this.get(id);
        if (travel == null) {
            throw new LogicException("参数异常");
        }
        if (travel.getState() != Travel.STATE_WAITING) {
            throw new LogicException("该游记不是待审核状态");
        }
        if (state == Travel.STATE_RELEASE) {
            //1. 需要修改状态为审核通过状态,
            travel.setState(Travel.STATE_RELEASE);
            //2. 修改发布时间,
            travel.setReleaseTime(new Date());
            //3. 最后更新时间,
            travel.setLastUpdateTime(new Date());
            //4. 记录审核信息(审核人,审核时间,状态,备注)
            this.update(travel);
        } else if (state == Travel.STATE_REJECT) {
            //1. 需要修改状态为审核通过状态,
            travel.setState(Travel.STATE_REJECT);
            //2. 修改发布时间,
            travel.setReleaseTime(null);
            //3. 最后更新时间,
            travel.setLastUpdateTime(new Date());
            //4. 记录审核信息(审核人,审核时间,状态,备注)
            this.update(travel);
        } else {
            throw new LogicException("状态异常");
        }
    }

    @Override
    public void updateState(String id, int state) {
        Travel travel = this.get(id);
        if (state == Travel.STATE_WAITING) {
            //1.将状态设为待审核
            travel.setState(Travel.STATE_WAITING);
            //2. 修改发布时间,
            travel.setReleaseTime(null);
            //3. 最后更新时间,
            travel.setLastUpdateTime(new Date());
            this.update(travel);
        }
    }

    @Override
    public List<Travel> queryViewNumTop3(String destId) {
        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");

        List<Travel> list = travelRepository.findByDestId(destId, pageable);
        return list;
    }

    @Override
    public List<Travel> queryByDestName(String destName) {
        List<Travel> travels = travelRepository.findByDestName(destName);
        for (Travel travel : travels) {
            if (travel!=null){
                travel.setAuthor(userInfoService.get(travel.getUserId()));
                travel.setDest(destinationService.get(travel.getDestId()));
            }
        }
        return travels;
    }
}
